#!/usr/py
# -*- coding: utf-8 -*-
# by ludc 2018/12/04

import configparser
import os
from datetime import datetime

from flask import Flask, request, jsonify
from flask_cors import *
from ps.myprint import MyPrint

app = Flask(__name__)
# 跨域设置
CORS(app, supports_credentials=True)


@app.route('/')
def hello_word():
    time_str = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
    return time_str


@app.route('/dc', methods=['GET', 'POST'])
def api_mes():
    result = {}
    result['status'] = 0
    result['message'] = "错误"
    result['data'] = ""
    if request.method == 'POST':
        if not request.json or 'c' not in request.json or 't' not in request.json:
            result['message'] = "参数不正确"
        else:
            c = request.json['c']
            t = request.json['t']
            ret = myps.doprint(c, t)
            result['message'] = ret
            result['data'] = ret
    else:
        result['message'] = '方法不正确'
    return jsonify(result)


if __name__ == "__main__":
    # 获取当前路径
    filepath = os.path.dirname(os.path.realpath(__file__))
    cf = configparser.ConfigParser()
    cf.read(filepath + "\config.ini")
    ip = cf.get("Server", "ip")
    port = cf.get("Server", "port")
    myps = MyPrint()
    app.run(host=ip, port=port)
